Table LevelFuncs
A hierarchical table system for organizing level-specific functions.
This serves a few purposes: it holds the predefined level callbacks documented below, as well as any trigger functions you might have specified.
For example, if you give a trigger a Lua name of "my_trigger" in Tomb Editor, you will have to implement it as a member of this table:
LevelFuncs.my_trigger = function() -- implementation goes here end
You can organize functions into tables within the hierarchy:
LevelFuncs.enemyFuncs = {}
LevelFuncs.enemyFuncs.makeBaddyRunAway = function()
-- implementation goes here
end
LevelFuncs.enemyFuncs.makeBaddyUseMedkit = function()
-- implementation goes here
end
Notes
- LevelFuncs is created automatically. Never assign a value to LevelFuncs itself, as that will overwrite the entire table.
For example, do NOT do this:LevelFuncs = {} -- This will break everything! LevelFuncs = LevelFuncs -- not needed, LevelFuncs already exists. - LevelFuncs.External is for 'third-party' functions.
For example, if you write a library providing LevelFuncs functions for other builders to use in their levels, put those functions in:LevelFuncs.External.YourLibraryNameHere = {} LevelFuncs.External.YourLibraryNameHere.yourFunction = function() -- implementation goes here end LevelFuncs.External.YourLibraryNameHere.yourFunction2 = function() -- implementation goes here end - LevelFuncs.Engine is a reserved table used internally by TombEngine's libs. Do not modify, overwrite, or add to it.
Predefined callbacks
The level's .lua file contains predefined callback members that are called on
specific events in the level. If you want to execute code or call other
functions on these events, place that logic inside the corresponding callback.
The order of loading is as follows:
- The level data itself is loaded.
- The level script itself is run (i.e. any code you put outside the LevelFuncs callbacks is executed).
- Save data is loaded, if loading from a saved game. This will empty LevelVars and GameVars and repopulate them with what they contained when the game was saved.
- If loading from a save, OnLoad will be called. Otherwise, OnStart will be called.
- The control loop, in which OnLoop is called once per frame, begins.
| OnStart() | Called when a level is entered by completing a previous level or by selecting it in the menu. |
| OnLoad() | Called when a level is loaded from a saved game. |
| OnLoop() | Called once per frame while the level is active. |
| OnSave() | Called when the game is saved while in the level. |
| OnEnd(reason) | Called when leaving a level. |
| OnUseItem(itemID) | Called when the player uses an item from their inventory. |
| OnPickup(pickup) | Called when a pickup is added to the inventory. |
| OnVehicleEnter(vehicle) | Called after player enters a vehicle. |
| OnVehicleLeave(vehicle) | Called after player exits a vehicle. |
| OnFreeze() | Called when any of the Flow.FreezeMode is activated. |
- OnStart()
-
Called when a level is entered by completing a previous level or by selecting it in the menu.
Will not be called when loaded from a saved game.
Usage:
LevelFuncs.OnStart = function() -- implementation goes here end
- OnLoad()
-
Called when a level is loaded from a saved game.
Usage:
LevelFuncs.OnLoad = function() -- implementation goes here end
- OnLoop()
-
Called once per frame while the level is active.
Usage:
LevelFuncs.OnLoop = function() -- implementation goes here end
- OnSave()
-
Called when the game is saved while in the level.
Usage:
LevelFuncs.OnSave = function() -- implementation goes here end
- OnEnd(reason)
-
Called when leaving a level.
This includes finishing it, exiting to the menu, or loading a save in a different level.
Parameters:
- reason EndReason A reason why level has ended.
Usage:
LevelFuncs.OnEnd = function(reason) if reason == TEN.Logic.EndReason.LEVEL_COMPLETE then -- implementation goes here end if reason == TEN.Logic.EndReason.DEATH then print("death") end end
- OnUseItem(itemID)
-
Called when the player uses an item from their inventory.
Parameters:
- itemID ObjID Object ID of an item that was used.
Usage:
LevelFuncs.OnUseItem = function(itemID) if itemID == TEN.Objects.ObjID.SMALLMEDI_ITEM then -- implementation goes here end end
- OnPickup(pickup)
-
Called when a pickup is added to the inventory.
Parameters:
- pickup Moveable Moveable pickup item that was picked up.
Usage:
LevelFuncs.OnPickup = function(pickup) if pickup:GetObjectID() == TEN.Objects.ObjID.FLARE_ITEM then -- implementation goes here end end
- OnVehicleEnter(vehicle)
-
Called after player enters a vehicle.
Parameters:
- vehicle Moveable A vehicle that was mounted.
Usage:
LevelFuncs.OnVehicleEnter = function(vehicle) -- implementation goes here end
- OnVehicleLeave(vehicle)
-
Called after player exits a vehicle.
Parameters:
- vehicle Moveable A vehicle that was dismounted.
Usage:
LevelFuncs.OnVehicleLeave = function(vehicle) -- implementation goes here end
- OnFreeze()
-
Called when any of the Flow.FreezeMode is activated.
Usage:
LevelFuncs.OnFreeze = function() -- implementation goes here end
